package com.lykj.llw.box.ui.activity

import android.content.Intent
import android.os.Bundle
import android.text.TextUtils
import android.view.View
import android.widget.TextView
import androidx.recyclerview.widget.LinearLayoutManager
import app.dinus.com.itemdecoration.LinearDividerItemDecoration
import com.blankj.utilcode.util.GsonUtils
import com.blankj.utilcode.util.LogUtils
import com.gyf.immersionbar.BarHide
import com.gyf.immersionbar.ImmersionBar
import com.lykj.lib_base.app.BaseApplication
import com.lykj.lib_base.base.BaseActivity
import com.lykj.lib_base.internal.extfun.clickWithTrigger
import com.lykj.llw.box.R
import com.lykj.llw.box.adapter.GameGiftBagAdapter
import com.lykj.llw.box.data.bean.response.GameDetailResponse
import com.lykj.llw.box.databinding.ActGameGiftBagBinding

/**
 * ================================================
 * 游戏礼包
 * ================================================
 *
 * @author gosha
 * @date 2023/10/30-09:57
 */
class GameGiftBagActivity : BaseActivity() {
    lateinit var binding: ActGameGiftBagBinding

    private val mAdapter = GameGiftBagAdapter()
    private var mGiftBagData = ArrayList<GameDetailResponse.DataDTO.CardsDTO>()

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)

        binding = ActGameGiftBagBinding.inflate(layoutInflater)
        setContentView(binding.root)

        initUI()
        initData()
        setListener()
    }

    override fun onResume() {
        super.onResume()

        ImmersionBar.with(this).transparentBar()
            .statusBarDarkFont(true) //状态栏字体是深色，不写默认为亮色
            .navigationBarDarkIcon(true)
            .hideBar(BarHide.FLAG_HIDE_NAVIGATION_BAR)
            .navigationBarEnable(false)
            .init()
    }

    override fun initUI() {
        if (!TextUtils.isEmpty(intent.getStringExtra("json"))) {
            val data = intent.getStringExtra("json")
            val bean = GsonUtils.fromJson(data, GameDetailResponse::class.java)

            mGiftBagData.addAll(bean.data.cards)
        }

        binding.titleBar.tvTitle.text = "游戏礼包"
        binding.titleBar.tvRightOption.text = "我的礼包"

        initRCV()
    }

    private fun initRCV() {
        val linearLayoutManager = LinearLayoutManager(this)
        linearLayoutManager.orientation = LinearLayoutManager.VERTICAL
        binding.rcvGift.layoutManager = linearLayoutManager
        binding.rcvGift.setHasFixedSize(true)
        mAdapter.data = mGiftBagData

        val emptyView = View.inflate(this, R.layout.empty_playing, null)
        emptyView.findViewById<TextView>(R.id.tv_desc).text = "暂无游戏礼包，晚点再来哦！"

        mAdapter.setEmptyView(emptyView)
        mAdapter.setOnItemClickListener { _, _, position ->
            val intent = Intent(this, GiftDetailActivity::class.java)
            intent.putExtra("cid", mGiftBagData[position].cid)
            startActivity(intent)
        }
        binding.rcvGift.adapter = mAdapter
    }

    override fun initData() {
    }

    override fun setListener() {
        binding.titleBar.ivBack.clickWithTrigger(500) {
            finish()
        }

        binding.titleBar.tvRightOption.clickWithTrigger(500) {
            if (null != BaseApplication.instance?.getUserInfo()) {
                startActivity(Intent(this, MyGiftBagActivity::class.java))
            } else {
                startActivity(
                    Intent(
                        this@GameGiftBagActivity,
                        MobileLoginActivity::class.java
                    )
                )
            }
        }
    }

    override fun onDestroy() {
        super.onDestroy()
    }
}